package com.sgb.kjwl.viewmodel.demand;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.DemandPublishEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.event.self.DemandEvent;
import com.sgb.kjwl.view.ui.activity.demand.DemandDetailsActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;


import org.greenrobot.eventbus.EventBus;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.swgk.core.event.EventConstant.EVENT_ADD_GOOGS_DEL;
import static com.swgk.core.event.EventConstant.EVENT_ADD_GOOGS_DOWN;


public class DemandModel3 extends BaseViewModel {
    public ObservableField<DemandPublishEntity> demandPublishEntity = new ObservableField<>(new DemandPublishEntity());
    private DemandDetailsActivity activity;
    private DemandEvent demandEvent;
    private HomeRepertory repertory;

    public DemandModel3(DemandDetailsActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //需求详情
    public void getNeedsList(String no) {
        DialogHelper.showProgressDialog(activity.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().getNeedsDetail(no).enqueue(new Callback<BaseEntity<DemandPublishEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<DemandPublishEntity>> call, Response<BaseEntity<DemandPublishEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    activity.setDatas(response.body().getData());
                } else {
                    MToast.showToast(activity, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<DemandPublishEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    //需求上下架
    public void upDown(String no, String status) {
        DialogHelper.showProgressDialog(activity.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().upDown(no, status).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    MToast.showToast(activity, response.body().getData().toString());
                    if (demandEvent == null) {
                        demandEvent = new DemandEvent();
                    }
                    if ("1".equals(status)) {
                        activity.setString("下架");
                        activity.setString2("已上架");
                    } else {
                        activity.setString("上架");
                        activity.setString2("已下架");
                    }

                    demandEvent.setMessageTag(EVENT_ADD_GOOGS_DOWN);
                    EventBus.getDefault().post(demandEvent);
//                    activity.finish();
                } else {
                    MToast.showToast(activity, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    //需求删除
    public void delete(String no) {
        DialogHelper.showProgressDialog(activity.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().delete(no).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    MToast.showToast(activity, response.body().getData().toString());
                    if (demandEvent == null) {
                        demandEvent = new DemandEvent();
                    }
                    demandEvent.setMessageTag(EVENT_ADD_GOOGS_DEL);
                    EventBus.getDefault().post(demandEvent);
                    activity.finish();
                } else {
                    MToast.showToast(activity, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
}
